package com.onehash.onebilling.entity;


/*
 * Author : Xia Xi
 * 
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;

import com.onehash.onebilling.entity.User.*;


public class UserRegister  implements Serializable{

	private HashMap <String, User> users;
	private Observer userObserver; //for display use
	private User selectedUser;
	private static UserRegister userRegisterInstance;
    
	private UserRegister () throws FileNotFoundException, IOException{
		//load from file
		 OneBillingStorage storage=OneBillingStorage.getInstance();
		 users = new HashMap <String, User>();
		 /*for first time run
		 addUser(EnumUserRole.SUPERADMIN, "super", "haha");
		 addUser(EnumUserRole.ADMIN, "admin", "haha");
		 addUser(EnumUserRole.AGENT, "agent", "haha");
		 addUser(EnumUserRole.ADMIN, "a", "a");
		 addUser(EnumUserRole.AGENT, "b", "b");
		 saveFile();
		 */
		 users=storage.readUsersObjects(storage.USERS_OBJ_FILENAME);
	}
	
	public static UserRegister getInstance() throws FileNotFoundException, IOException{
        if(userRegisterInstance==null){
        	userRegisterInstance=new UserRegister();
        }
        return userRegisterInstance;
    }
	
	public HashMap<String, User> getUsers() {
		return users;
	}
	
	
	public void setUsers(HashMap<String, User> users) {
		this.users = users;
	}
	
	
	public User getUser (String userName){
		if (users.containsKey(userName)){
			
			return users.get(userName);
			
		}else {
			
			return null;
		}
	}
	public User getSelectedUser() {
		return selectedUser;
	}
	
	/* Adds an observer UI class */
    public void addObserver(Observer UI){
        userObserver = UI;
    }
    
	/* Method call to perform display user */
    public boolean displayUser( String userName ) {
        selectedUser = users.get(userName);

        if( selectedUser == null ) //user not found
            return false;
        else{
            userObserver.update();
            return true;
        }
    }
	
    
	public User addUser (EnumUserRole role, String userName, String password)throws FileNotFoundException, IOException{
		if (getUser(userName) == null) {
			User user = new User(role, userName, password);
			users.put(userName, user);
			saveFile(); 			//save to file
			return user;
		}else {
			return null;
		}
		
	}
	
	public boolean checkUserNamePassword (String userName, String password) {
		User user = getUser(userName);
		if (user != null) {
			if (password != null && password.length()>0) {
				return user.isCorrectPassword(password);
				
			}
		}
		return false;
	}
	
	public boolean changePassword (String userName, String oldPassword, String newPassword)throws FileNotFoundException, IOException{
		if (checkUserNamePassword(userName,oldPassword)){
			User user = getUser(userName);
			user.setPassword(newPassword);
			saveFile();//save to file
			return true;
		}
		
		return false;
		
	}

	public void removeUser(String userName)throws FileNotFoundException, IOException {
		if (getUser(userName) == null) {
			return;
		}else {
			users.remove(userName);
			saveFile();
		}
		
	}
	
	private void saveFile()throws FileNotFoundException, IOException{
		  OneBillingStorage storage=OneBillingStorage.getInstance();
          storage.writeUsersObjects(users, storage.USERS_OBJ_FILENAME);
	}

	public String [] getUserNameArr(){
		Set <String> userNames = users.keySet();
		String [] nameArr = (String[])userNames.toArray();
		return nameArr;
	
	}
	
	public void show(){
		System.out.println ("************ User List **************");
		System.out.println(getUserNameArr());
	}
}
